using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace UserLoginSystem
{
    class Demo2
    {
        class User
        {
            public string Username { get; set; }
            public string Password { get; set; }
            
            public User(string username, string password)
            {
                Username = username;
                Password = password;
            }
        }
        static List<User> users = new List<User>();
        
        static void Main(string[] args)
        {
            Console.WriteLine("=== 用户管理系统 ===");
            ShowMainMenu();
        }
        static void ShowMainMenu()
        {
            while (true)
            {
                Console.WriteLine("\n请选择操作：");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请输入数字选择：");

                string choice = Console.ReadLine()?.Trim();

                switch (choice)
                {
                    case "1":
                        Login();
                        break;
                    case "2":
                        Register();
                        break;
                    case "3":
                        Console.WriteLine("感谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("请输入有效数字（1-3）！");
                        break;
                }
            }
        }
        static void Register()
        {
            Console.WriteLine("\n=== 用户注册 ===");
            string username;
            while (true)
            {
                Console.Write("请输入用户名：");
                username = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }
                if (users.Any(u => u.Username == username))
                {
                    Console.WriteLine("用户名已存在，请重新输入！");
                    continue;
                }
                if (!ValidateUsername(username))
                {
                    Console.WriteLine("用户名格式错误！长度必须为3-15个字符，只能包含字母、数字、下划线。");
                    continue;
                }

                break;
            }
            string password;
            while (true)
            {
                Console.Write("请输入密码：");
                password = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }

                if (!ValidatePassword(password))
                {
                    Console.WriteLine("密码格式错误！长度必须为6-20个字符，且包含至少1个大写字母、1个小写字母和1个数字。");
                    continue;
                }

                break;
            }
            string confirmPassword;
            while (true)
            {
                Console.Write("请确认密码：");
                confirmPassword = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(confirmPassword))
                {
                    Console.WriteLine("确认密码不可为空！");
                    continue;
                }

                if (password != confirmPassword)
                {
                    Console.WriteLine("两次输入的密码不一致，请重新输入！");
                    continue;
                }

                break;
            }
            User newUser = new User(username, password);
            users.Add(newUser);
            
            Console.WriteLine($"\n注册成功！欢迎 {username}！");
            Console.WriteLine("自动返回主菜单...");
        }
        static void Login()
        {
            Console.WriteLine("\n=== 用户登录 ===");

            if (users.Count == 0)
            {
                Console.WriteLine("暂无注册用户，请先注册！");
                return;
            }

            string username;
            while (true)
            {
                Console.Write("请输入用户名：");
                username = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }

                break;
            }
            User user = users.FirstOrDefault(u => u.Username == username);
            if (user == null)
            {
                Console.WriteLine("用户名不存在！");
                return;
            }

            string password;
            while (true)
            {
                Console.Write("请输入密码：");
                password = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }

                break;
            }
            if (user.Password == password)
            {
                Console.WriteLine($"\n登录成功！欢迎回来，{username}！");
                ShowUserHomePage(username);
            }
            else
            {
                Console.WriteLine("密码错误！");
            }
        }
        static void ShowUserHomePage(string username)
        {
            Console.WriteLine($"\n=== 系统首页 - 欢迎 {username} ===");
            Console.WriteLine("这里是系统主页，可以在这里添加更多功能...");
            
            while (true)
            {
                Console.WriteLine("\n请选择操作：");
                Console.WriteLine("1. 查看个人信息");
                Console.WriteLine("2. 修改密码");
                Console.WriteLine("3. 退出登录");
                Console.Write("请输入数字选择：");

                string choice = Console.ReadLine()?.Trim();

                switch (choice)
                {
                    case "1":
                        ShowUserInfo(username);
                        break;
                    case "2":
                        ChangePassword(username);
                        break;
                    case "3":
                        Console.WriteLine("已退出登录，返回主菜单...");
                        return;
                    default:
                        Console.WriteLine("请输入有效数字（1-3）！");
                        break;
                }
            }
        }
        static void ShowUserInfo(string username)
        {
            User user = users.FirstOrDefault(u => u.Username == username);
            if (user != null)
            {
                Console.WriteLine($"\n=== 个人信息 ===");
                Console.WriteLine($"用户名：{user.Username}");
                Console.WriteLine($"密码：{new string('*', user.Password.Length)}");
            }
        }
        static void ChangePassword(string username)
        {
            Console.WriteLine("\n=== 修改密码 ===");
            
            User user = users.FirstOrDefault(u => u.Username == username);
            if (user == null) return;

            string currentPassword;
            while (true)
            {
                Console.Write("请输入当前密码：");
                currentPassword = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(currentPassword))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }

                if (user.Password != currentPassword)
                {
                    Console.WriteLine("当前密码错误！");
                    return;
                }

                break;
            }

            string newPassword;
            while (true)
            {
                Console.Write("请输入新密码：");
                newPassword = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(newPassword))
                {
                    Console.WriteLine("新密码不可为空！");
                    continue;
                }

                if (!ValidatePassword(newPassword))
                {
                    Console.WriteLine("新密码格式错误！长度必须为6-20个字符，且包含至少1个大写字母、1个小写字母和1个数字。");
                    continue;
                }

                break;
            }

            user.Password = newPassword;
            Console.WriteLine("密码修改成功！");
        }
        static bool ValidateUsername(string username)
        {
            if (username.Length < 3 || username.Length > 15)
                return false;

            return Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$");
        }
        static bool ValidatePassword(string password)
        {
            if (password.Length < 6 || password.Length > 20)
                return false;
            bool hasUpper = false;
            bool hasLower = false;
            bool hasDigit = false;

            foreach (char c in password)
            {
                if (char.IsUpper(c)) hasUpper = true;
                if (char.IsLower(c)) hasLower = true;
                if (char.IsDigit(c)) hasDigit = true;
            }

            return hasUpper && hasLower && hasDigit;
        }
    }
}